"use strict";
cc._RF.push(module, '4f65cBVCFlEQqO0L4FvRnkF', 'HttpUtil');
// resources/Script/Ssg/Utils/HttpUtil.js

'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var ProtoBufUtil = require('ProtoBufUtil');
var QQHttp = require('QQHttp');
var NativeHttp = require('NativeHttp');

window.HttpUtil = function () {

    var ip = "http://127.0.0.1:8085",
        errorCode = null;

    var _static = {

        setIp: function setIp(ip) {
            this.ip = ip;
        },

        setErrorCode: function setErrorCode(errorCode) {
            this.errorCode = errorCode;
        },

        /**
         * by     黄新宇
         * proto  get请求方式     最佳适配：nafos服务端
         * @param returnProtoUrl  接受返回值的protoUrl
         * @param returnProtoName  接受返回值的Message
         * @param uri              请求路径
         * @param callback         回调
         */
        nafosBinaryGet: function nafosBinaryGet(returnProtoUrl, returnProtoName, uri, callback) {
            var self = this;
            if (!uri || uri == '') {
                return;
            }
            var url = this.ip + uri;
            UIHelper.showLoaing();
            if (SystemInfo.getPlatform() == 'qqPlay') {
                //QQ渠道
                QQHttp.get(url, function (isTimeOut, obj) {
                    self.qReturnCallBack(url, returnProtoUrl, returnProtoName, isTimeOut, obj, callback);
                });
                return;
            }

            /**
             * 中间扩充其他特殊API渠道。记得return
             */

            // 最后是JS原生渠道，能用XHR的
            NativeHttp.get('arraybuffer', url, function (isTimeOut, xhr) {
                self.returnCallBack(url, returnProtoUrl, returnProtoName, isTimeOut, xhr, callback);
            });
        },

        nafosJsonGet: function nafosJsonGet(uri, callback) {
            var self = this;
            if (!uri || uri == '') {
                return;
            }
            var url = this.ip + uri;
            UIHelper.showLoaing();
            if (SystemInfo.getPlatform() == 'qqPlay') {
                //QQ渠道
                QQHttp.get(url, function (isTimeOut, obj) {
                    if (isTimeOut) {
                        self.doTimeOut(url);
                        return;
                    }
                    var body = obj.text();
                    if (obj && body) {
                        var type = typeof body === 'undefined' ? 'undefined' : _typeof(body);
                        if (type == 'string') {
                            body = JSON.parse(body);
                            callback(body);
                        }
                    }
                });
                return;
            }

            /**
             * 中间扩充其他特殊API渠道。记得return
             */

            // 最后是JS原生渠道，能用XHR的
            NativeHttp.get('arraybuffer', url, function (isTimeOut, xhr) {
                if (isTimeOut) {
                    self.doTimeOut(url);
                    return;
                }
                var type = _typeof(xhr.response);
                if (type == 'string') {
                    xhr.response = JSON.parse(xhr.response);
                }
                callback(xhr.response);
            });
        },

        nafosBinaryPost: function nafosBinaryPost(uri, param, sendProtoUrl, sendProtoName, returnProtoUrl, returnProtoName, callback) {
            // 地址 , 结果返回
            var newUrl = this.ip + uri;
            var self = this;
            UIHelper.showLoaing();

            ProtoBufUtil.protoEncode(sendProtoUrl, sendProtoName, param, function (buffer) {
                //QQ post
                if (SystemInfo.getPlatform() == 'qqPlay') {
                    //QQ渠道
                    QQHttp.post(newUrl, buffer, returnProtoUrl, returnProtoName, function (isTimeOut, obj) {
                        self.qReturnCallBack(newUrl, returnProtoUrl, returnProtoName, isTimeOut, obj, callback);
                    });
                    return;
                }

                //原生
                NativeHttp.post('arraybuffer', newUrl, buffer, function (isTimeOut, xhr) {
                    self.returnCallBack(newUrl, returnProtoUrl, returnProtoName, isTimeOut, xhr, callback);
                });
            });
        },

        /**
         * 消息返回统一处理
         * @param isTimeOut
         * @param xhr
         * @param callBack
         */
        returnCallBack: function returnCallBack(url, returnProtoUrl, returnProtoName, isTimeOut, xhr, callback) {
            if (isTimeOut) {
                this.doTimeOut(url);
                return;
            }
            if (xhr && xhr.response) {
                ProtoBufUtil.protoDecode(returnProtoUrl, returnProtoName, xhr.response, callback);
            }
        },
        qReturnCallBack: function qReturnCallBack(url, returnProtoUrl, returnProtoName, isTimeOut, obj, callback) {
            if (isTimeOut) {
                self.doTimeOut(url);
                return;
            }
            var body = obj.arrayBuffer();
            if (obj && body) {
                ProtoBufUtil.protoDecode(returnProtoUrl, returnProtoName, body, callback);
            }
        },

        /**
         * 请求超时处理
         * @param url
         */
        doTimeOut: function doTimeOut(url) {
            Log.error('--------------请求超时：', url);
            UIHelper.hideLoaing();
            var tipsMsg = '网络连接超时';
            AddPrefabHelper.addMsgTipsUI(tipsMsg, true);
        },

        /**
         * 错误码
         * @param status
         * @returns {string}
         */
        getTextWithStatus: function getTextWithStatus(status) {
            status = status.toString();
            return errorCode[status] ? errorCode[status] : "系统异常";
        }

    };
    return _static;
}();

module.exports = HttpUtil;

cc._RF.pop();